package com.ybkj.o2o.zhuanxian.common;

import com.ybkj.o2o.zhuanxian.common.config.DaijiaApi;
import com.ybkj.o2o.zhuanxian.manager.OrderManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时器
 */

@Component
@Configuration
@EnableScheduling
public class SaticScheduleTask {

    @Autowired
    private DaijiaApi daijiaApi;
    @Autowired
    private OrderManager orderManager;

    /**
     * 每日凌晨00:00:00定时器
     */
    private static final String CRON_DAY = "0 0 0 * * ?";

    /**
     * 定时生成一条最大红包的生成规则
     * @throws Exception
     */
    @Scheduled(cron = CRON_DAY)
    public void scheduling() throws Exception{
        Map<String,Object> cycleMap = new HashMap<>();
        cycleMap.put("created",new Date());
        Map<String,Object> redPacketMap = (Map)daijiaApi.isRandomRedPacket("cycle");
        String cycle = redPacketMap.get("cycle").toString();
        String startDate;
        String endDate;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //生成日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //获得年
        Integer year = calendar.get(Calendar.YEAR);
        //获得月
        Integer month = calendar.get(Calendar.MONTH) + 1;
        //一个月多少号
        Integer dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        startDate =  year + "-" + month + "-" + dayOfMonth + " 00:00:00";
        Integer maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        endDate =  year + "-" + month + "-" + maxDay + " 23:59:59";
        //周期为月
        if(cycle.equals("month")){
            cycleMap.put("startDate", sdf.parse(startDate));
            cycleMap.put("endDate", sdf.parse(endDate));
            cycleMap.put("cycle","month");
            cycleMap.put("redPacketDate",new RandomRedPacket().getCycle(maxDay));
            cycleMap.put("randomRedPacket",new Double(50));
            //每月1号生成一条记录
            if(1 == dayOfMonth){
                orderManager.insertCycleRecord(cycleMap);
            }
        } else { //周期为周
            //获取周几
            Integer dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            //把周一设为一周第一天
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            //
            dayOfWeek = 1 == dayOfWeek ? 8 : dayOfWeek;
            //根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
            calendar.add(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() - dayOfWeek);
            //本周周一日期
            startDate = year + "-" + month + "-" + calendar.get(Calendar.DAY_OF_MONTH) + " 00:00:00";
            cycleMap.put("startDate", sdf.parse(startDate));
            //周一加6天
            maxDay = calendar.get(Calendar.DAY_OF_MONTH) + 6;
            //本周周天日期
            endDate = year + "-" + month + "-" + maxDay + " 23:59:59";
            cycleMap.put("endDate", sdf.parse(endDate));
            cycleMap.put("cycle","week");
            cycleMap.put("redPacketDate",new RandomRedPacket().getCycle(7));
            cycleMap.put("randomRedPacket",new Double(50));
            //每周一生成一条记录
            if(8 == dayOfWeek){
                orderManager.insertCycleRecord(cycleMap);
            }

        }

    }

}
